const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      dialogVisible: false,
      flag: true,
      enterprise: [],
      num: '',
      value1: '',
      article_info: {},//选择的商品
      cardCategory: [
        {
          name: '购物卡',
          value: 0
        },
      ],
      dateCategory: [
      ],
      PurchaseRestrictions: [
        {
          name: '叠加购买',
          value: 0
        },
        {
          name: '限购',
          value: 1
        },
      ],
      value1: [],
      form: {
        Card_SerialNumber: '',//主键
        Card_Name: "",//卡名
        Card_Type: "",//卡类别
        Card_Describe: "",//卡描述
        Card_Amount: "",//卡面额
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),//所属企业
        Children: {
          card_article: [{
            Card_Article_SerialNumber: "",//子表主键
            Card_SerialNumber: '',//主表主键
            Article_SerialNumber: "",//商品主键
            Article_Specifications_SerialNumber: "",//规格主键
            Unit_Price: "",//商品原价格
            Card_Term: "",//商品有效期
            Time_Unit_SerialNumber: "",//商品有效期单位
            Start_Date:"",//商品开始销售时间
            Quantity:"",//数量
            Preferential_Unit_Price: "",//商品优惠价
          }],
          card_storescope: [
            {
              Card_StoreScope_SerialNumber: "",//子表主键
              Card_SerialNumber: '',//主表主键
              Store_SerialNumber: "",//门店流水号
              App_ID: "",//小程序id
            }
          ]
        }
      },
      store: [],
      shop: [],
      specs: [],
      total:0,
      isDisabled: true,
      loading: false
    }
  },
  mounted() {
    this.getEnterprise()
    this.getShopData()
    this.getData()
    this.getShop()
    this.getDate()
  },
  computed: {
    //该计算属性返回要监视的对象属性值
    infoa() {
      return this.form.Total_Amount;
    }
  },
  watch: {
    //开始监听返回该对象属性值计算属性
    infoa: function (newVal) {
      if (newVal == '' || newVal == 0) {
        this.flag = true
      } else {
        this.flag = false
      }
    }
  },
  methods: {
    getEnterprise() {
      let _this = this;
      $.ajax({
        url: DomainName + "/GoodsFileInquiry",
        type: 'POST',
        data: { Data: "enterprise_info" },
        success: function (res) {
          _this.enterprise = res
          console.log(_this.enterprise)
        },
        error: function () {
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getDate(){
      var _this = this
      $.ajax({
        url: DomainName + "/GoodsFileInquiry",
        type: 'POST',
        data: { Data: "time_unit" },
        success: function (res) {
          console.log(res)
          _this.dateCategory = res
        },
        error: function () {
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    add() {
      this.form.Children.card_storescope.push({
        Card_StoreScope_SerialNumber: "",//子表主键
        Card_SerialNumber: '',//主表主键
        Store_SerialNumber: "",//门店流水号
        App_ID: "",//小程序id
      })
    },
    add1() {
      this.form.Children.card_article.push({
        Card_Article_SerialNumber: "",//子表主键
        Card_SerialNumber: '',//主表主键
        Article_SerialNumber: "",//商品主键
        Article_Specifications_SerialNumber: "",//规格主键
        Unit_Price: "",//商品原价格
        Preferential_Unit_Price: "",//商品优惠价
      })
    },
    remove() {
      if (this.form.Children.card_storescope.length > 1) {
        this.form.Children.card_storescope.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    remove1() {
      if (this.form.Children.card_article.length > 1) {
        this.form.Children.card_article.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    getData() {//获取该企业下的所有店铺
      let _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: 'store_info',
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          _this.store = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getShop() {
      var _this = this
      $.ajax({
        url: DomainName + '/getArticleInfo2Action',
        type: 'POST',
        data: { token: $.cookie("token") },
        success: function (res) {
          console.log(res)
          res = res.filter(function (item) {
            return item.Row_Status == '0'
          })
          res.forEach(item => {
            item.Article_Name = item.Article_Name + '(所属企业:' + item.Enterprise_Name + ')'
          })
          for (let i = 0; i < res.length; i++) {
            _this.shop.push(res[i]);
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    Change_Unit_Price(index) {
      let str = this.form.Children.card_article[index].Article_SerialNumber
      this.form.Children.card_article[index].Article_Specifications_SerialNumber = ''
      let _this = this;
      $.ajax({
        type: 'POST',
        url: DomainName + "/SpecificationsIDAction",
        data: {
          Article_SerialNumber: str,
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          _this.specs = res
          _this.isDisabled = false
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    Change_Specifications_Name(arr,id,index) {
      console.log(arr)
      arr.forEach(item=>{
        if(item.Article_Specifications_SerialNumber == id){
          this.form.Children.card_article[index].Unit_Price = item.Unit_Price
        }
      })
    },
    Article() {
      return function (name) {
        let obj = this.shop.filter((item, index) => {
          return name == item.Article_Name
        })[0]
        return obj.Article_SerialNumber
      }
    },
    Unit_Price() {
      return function (name) {
        let obj = this.shop.filter((item, index) => {
          return name == item.Article_Name
        })[0]
        return obj.Unit_Price
      }
    },
    getShopData() {// 获取该企业全部商品
      let _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: "article_info",
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          _this.article_info = res
        }
      })
    },
    change(e) {//输入框选择哪个商品就出现该商品的图片跟价格
      this.article_infos = this.article_info.filter(item => {
        return item.Article_SerialNumber == e
      })[0]
    },
    Change_Store_SerialNumber(index) {  //选择店铺获取对应App_ID
      let _this = this
      $.ajax({
        url: DomainName + '/getprepaymentSchemeAppIDAction',
        type: 'POST',
        data: {
          Store_SerialNumber: _this.form.Children.card_storescope[index].Store_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          if (res.length == 1) {
            _this.form.Children.card_storescope[index].App_ID = res[0].App_ID
          } else {
            _this.$message.error("该店铺未绑定App_ID")
            _this.form.Children.card_storescope[index].App_ID = ''
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm() {
      console.log(this.form)
      var _this = this
      if(!this.loading){
        this.loading = true
        $.ajax({
          url: DomainName + '/getAddAction',
          type: 'POST',
          data: {
            Data: JSON.stringify({ "card_info": [_this.form] }),
            token: $.cookie("token")
          },
          success: function (res) {
            console.log(res)
            _this.loading = false
            if (res.Status == 'success') {
              _this.$notify({
                title: '成功',
                message: '增加成功',
                type: 'success'
              });
              window.history.go(-1);
            } else {
              _this.$notify.error({
                title: '错误',
                message: '系统错误..'
              });
            }
          }
        })
      }
    },
  }
})